//
//  TSServiceViewController.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSServiceViewController: TSBaseViewController {
    
    var tableView : UITableView!
    var messageField : UITextField!
    var phoneField : UITextField!
    var nameField : UITextField!
    var accountField : UITextField!
    
    
    @objc func commitAction() {
        
        if checkCommitCondition() {
            print("提交")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.delegate = self
        configViews()
//        setupNavigationTitle(title: "给我们留言")
    }
    
    func checkCommitCondition() -> Bool {
        
        guard messageField.text?.count != 0 else {
            CBToast.showToastAction(message: "请留言")
            return false
        }
        
        guard phoneField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入您的电话")
            return false
        }
        
        guard nameField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入您的称呼")
            return false
        }
        
        guard accountField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入支行地址")
            return false
        }
        
        return true
    }
    
    deinit {
        self.navigationController?.delegate = nil
    }
}

extension TSServiceViewController {
    
    func configViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: CGFloat(T_NAVBARHEIGHT), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - CGFloat(T_TABBARHEIGHT) - CGFloat(T_NAVBARHEIGHT)), style: .plain)
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(tableView)
        
        messageField = createInputField(placeholder: "请留言")
        tableView.addSubview(messageField)
        messageField.snp.makeConstraints { (make) in
            make.left.equalTo(26)
            make.top.equalTo(31)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*26, height: 50))
        }
        
        let line1 = drawLine()
        tableView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(messageField.snp_bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
                
        phoneField = createInputField(placeholder: "请输入您的电话")
        phoneField.keyboardType = .phonePad
        tableView.addSubview(phoneField)
        phoneField.snp.makeConstraints { (make) in
            make.left.equalTo(messageField)
            make.top.equalTo(line1.snp_bottom)
            make.size.equalTo(messageField)
        }
        
        let line2 = drawLine()
        tableView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(phoneField.snp_bottom)
            make.size.equalTo(line1)
        }
                        
        nameField = createInputField(placeholder: "请输入您的称呼")
        tableView.addSubview(nameField)
        nameField.snp.makeConstraints { (make) in
            make.left.equalTo(messageField)
            make.top.equalTo(line2.snp_bottom)
            make.size.equalTo(messageField)
        }
        
        let line3 = drawLine()
        tableView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(nameField.snp_bottom)
            make.size.equalTo(line1)
            }
                                
        accountField = createInputField(placeholder: "请输入qq账号")
        tableView.addSubview(accountField)
        accountField.snp.makeConstraints { (make) in
            make.left.equalTo(messageField)
            make.top.equalTo(line3.snp_bottom)
            make.size.equalTo(messageField)
        }
        
        let line4 = drawLine()
        tableView.addSubview(line4)
        line4.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(accountField.snp_bottom)
            make.size.equalTo(line1)
        }
        
        let commitBtn = UIButton.init(type: .system)
        commitBtn.setTitle("提交", for: .normal)
        commitBtn.setTitleColor(WHITE_COLOR, for: .normal)
        commitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        commitBtn.backgroundColor = BASE_PURPLE_COLOR
        commitBtn.layer.cornerRadius = 24
        commitBtn.layer.masksToBounds = true
        commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
        tableView.addSubview(commitBtn)
        commitBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(line4.snp_bottom).offset(50)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let attributeStr = NSMutableAttributedString.init(string: placeholder)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#989898"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: .medium)], range: NSMakeRange(0, placeholder.count))
        
        let textField = UITextField.init()
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}

extension TSServiceViewController : UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        let isShowHomePage = viewController.isKind(of: TSServiceViewController.self)
        self.navigationController?.setNavigationBarHidden(isShowHomePage, animated: false)
    }
}
